﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ASoft.WPF.ViewModel;
using ASoft.Base;
using GalaSoft.MvvmLight.Messaging;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Threading;

namespace ASoft.WPF.View
{
    /// <summary>
    /// RoomSingleControl.xaml 的交互逻辑
    /// </summary>
    public partial class RoomSingleControl : UserControl
    {
        DispatcherTimer tm = new DispatcherTimer();
        ViewModelLocator locator = new ViewModelLocator();
        public RoomSingleControl()
        {
            InitializeComponent();

            ai1.AnimationSecond = 10;
            ai2.AnimationSecond = 10;
            ai3.AnimationSecond = 1;

            Messenger.Default.Register<MessageBase>(this, "手动合拢湿帘泵", msg =>
            {
                ai0.IsDesc = true;
                ai0.Continue();
            });
            Messenger.Default.Register<MessageBase>(this, "手动展开湿帘泵", msg =>
            {
                ai0.IsDesc = false;
                ai0.Continue();
            });

            Messenger.Default.Register<MessageBase>(this, "手动打开灌溉", msg => ai3.Repeat());
            Messenger.Default.Register<MessageBase>(this, "手动关闭灌溉", msg => ai3.Clear());

            StartInit();

            tm.Tick += new EventHandler(tm_Tick);
            tm.Interval = TimeSpan.FromSeconds(2);
            tm.Start();
        }

        void tm_Tick(object sender, EventArgs e)
        {
            var contentViewModel = this.DataContext as RoomDataViewModel;
            if (contentViewModel == null)
            {
                return;
            }


            var currentViewMode = locator.Main.温室实时数据集合.SingleOrDefault(p => p.Id == contentViewModel.Id);
            if (currentViewMode == null)
            {
                return;
            }

            if (contentViewModel.实时数据最后更新时间 < contentViewModel.实时数据最后更新时间)
            {
                this.DataContext = currentViewMode;
                ChangePic();
            }
        }


        private void InitImages()
        {
            卷帘不喷水动画资源 = new FileImageSource(ConfigInfo.GetFullPath(@"image\卷帘不喷水\"));
            灌溉动画资源 = new FileImageSource(ConfigInfo.GetFullPath(@"image\灌溉\"));
            风杯动画资源 = new FileImageSource(ConfigInfo.GetFullPath(@"image\风杯\"));
            指针动画资源 = new FileImageSource(ConfigInfo.GetFullPath(@"image\指针\"));

            ai0.AnimatedSource = 卷帘不喷水动画资源;

            ai1.AnimatedSource = 风杯动画资源;
            ai1.Repeat();

            ai2.AnimatedSource = 指针动画资源;
            //ai2.Repeat();

            ai3.AnimatedSource = 灌溉动画资源;


            MoveRoom(true);
        }

        private void StartInit()
        {
            Thread t = new Thread((ThreadStart)delegate
            {
                try
                {
                    this.Dispatcher.Invoke((Action)delegate
                    {

                        InitImages();
                    });
                }
                catch (Exception ex)
                {
                    this.Dispatcher.Invoke((Action)delegate { throw ex; });
                }
            });
            t.Start();
        }

        AnimatedImageSource 灌溉动画资源 { get; set; }

        AnimatedImageSource 卷帘不喷水动画资源 { get; set; }

        AnimatedImageSource 风杯动画资源 { get; set; }

        AnimatedImageSource 指针动画资源 { get; set; }

        private void btnPrevious_Click(object sender, RoutedEventArgs e)
        {
            MoveRoom(false);
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            MoveRoom(true);
        }

        void MoveRoomOnly(bool next)
        {

            if (this.DataContext == null || !(this.DataContext is RoomDataViewModel))
            {
                this.DataContext = locator.Main.温室实时数据集合.FirstOrDefault();
                return;
            }
            var currentRoom = this.DataContext as RoomDataViewModel;
            if (next && currentRoom.Equals(locator.Main.温室实时数据集合.Last()))
            {
                this.DataContext = locator.Main.温室实时数据集合.First();
                return;
            }
            if (!next && currentRoom.Equals(locator.Main.温室实时数据集合.First()))
            {
                this.DataContext = locator.Main.温室实时数据集合.Last();
                return;
            }
            var index = locator.Main.温室实时数据集合.IndexOf(currentRoom);
            index = index + (next ? 1 : -1);

            currentRoom = locator.Main.温室实时数据集合[index];
            this.DataContext = currentRoom;
        }


        /// <summary>
        /// true:下一个,false:上一个
        /// </summary>
        /// <param name="next"></param>
        void MoveRoom(bool next)
        {
            MoveRoomOnly(next);
            ChangePic();
        }

        private void ChangePic()
        {
            var currentRoom = this.DataContext as RoomDataViewModel;
            if (currentRoom == null)
            {
                return;
            }

            ai0.GoZero();
            if (currentRoom.温室实时数据.灌溉加湿 == 0)
            {
                ai3.Source = null;
            }
            else
            {
                ai3.Repeat();
            }

            ai0.FrameIndex = currentRoom.温室实时数据.湿帘泵位置;

            if (ai2.FrameIndex != (int)locator.Main.气象数据.风向)
            {
                ai2.FrameIndex = (int)locator.Main.气象数据.风向;
            }
            var fs = 30 - (int)locator.Main.气象数据.风速;
            fs = fs < 1 ? 1 : fs;
            if (ai1.AnimationSecond != fs)
            {
                if (fs == 30)
                {
                    ai1.Pause();
                }
                else
                {
                    ai1.AnimationSecond = fs;
                    ai1.Repeat();
                }
            }
        }
    }
}
